<?php
    include_once '../module/inventory/config.inc.php';
    $inventory_config = new config();
    
    //动态获取库存
    
    if(empty($_GET['stock'])){
        echo '0';
        
        exit;
        
    }
    $stock=$_GET['stock'];
    
    $product=explode(',',$stock);
    
    foreach ($product as $item){
        
        $_t=explode('_', $item);
        
        $ids[]=$_t[0];
        $nums[$_t[0]]=$_t[1];
    }
    
    
    
    
    $scl='and product_sku_id in ('.implode(',', $ids).')';
    $scl2='and productid in ('.implode(',', $ids).')';

    
    
    
    
		
	$sql="SELECT product_sku_id, stocks,canbuyout FROM ".$inventory_config->db_prefix."products where 1 $scl order by id asc";
	//====================//zx 虚拟库存 canbuyout
	$db->query($sql);
	$returns=$db->getRows();
	
	/* if ($_GET['gettitle']){
	    $result_title='';
	    foreach ($returns as $item){
	       $temp[$item['id']]=$item['title'];  
	    }
	    
	    foreach ($ids as $item){
	        $result_title.=$temp[$item].'X'.$nums[$item].' ';
	    }
	    echo $result_title;
	    exit;
	} */
	
	
	//锁定的库存
	$sql="SELECT sum(number) as locked,productid FROM ".$inventory_config->db_prefix."orders where timestocked=0 $scl2 group by productid";
	$db->query($sql);
	$lock=$db->getRows();
	
	foreach ($lock as $item){
	    $locked[$item['productid']]=$item['locked'];
	}
	
	//==================================
	$result_stock=0;
	foreach ($returns as $item){
	    if ($nums[$item['id']]){
	        $item_stock=$item['stocks']-$item['canbuyout']-$locked[$item['id']];
	        $_t=floor($item_stock/$nums[$item['id']]);
	    }else{
	        $_t=0;
	    }
	    if($result_stock==0){
	        $result_stock=$_t;
	    }elseif($result_stock>$_t){
	        $result_stock=$_t;
	    }
	    
	    
	}
	echo $result_stock;
?>